package com.my.demo.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * openApi接口的url前缀
     */
    private static final String OPENAPI_URL_PREFIX = "/open/**";

    /**
     * 邮件API接口的URL前缀
     */
    private static final String EMAIL_API_URL_PREFIX = "/email/**";


    /**
     * 支持跨域
     * 不对外提供delete，put请求类型
     *
     * @auther 杨亚旭
     * @date 2019/8/7 11:31
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET",
                        "POST",
                        "OPTIONS"
                )
                .maxAge(3600 * 24);
    }


    /**
     * 解决问题：使用2.0.5版本的eureka客户端后rest接口返回数据结构xml
     * 该设置会把xml结构改回json
     *
     * @auther 杨亚旭
     * @date 2019/8/7 11:30
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        List<MediaType> mediaTypes = new ArrayList<>(16);
        mediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        mediaTypes.add(MediaType.APPLICATION_CBOR);
        mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        mediaTypes.add(MediaType.APPLICATION_JSON);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        converter.setSupportedMediaTypes(mediaTypes);
        converters.add(converter);
    }

    /**
     * 注入jwt拦截器
     *
     * @auther 杨亚旭
     * @date 2019/8/11 11:15
     */

}